package com.jxzyk.model;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

import com.core.photo.HackyViewPager;
import com.core.photo.PhotoView;
import com.core.ui.activity.BaseActivity;
import com.jxzyk.R;
import com.jxzyk.adapter.OutlineAdapter;
import com.jxzyk.util.BitmapHelp;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.view.annotation.ContentView;

@ContentView(R.layout.act_photo)
public class PhotoActivity extends BaseActivity {

	private String[] url = new String[] {};

	@Override
	public void initData(Bundle savedInstanceState) {
		if (savedInstanceState != null) {
			boolean isLocked = savedInstanceState.getBoolean(ISLOCKED_ARG,
					false);
			((HackyViewPager) mViewPager).setLocked(isLocked);
		}
		savedInstanceState = getIntent().getExtras();
		if (savedInstanceState != null) {
			url = savedInstanceState.getStringArray(OutlineAdapter.TAG);
		}
	}

	private static final String ISLOCKED_ARG = "isLocked";

	private ViewPager mViewPager;
	private MenuItem menuLockItem;

	@Override
	public void initUi() {
		mViewPager = (ViewPager) findViewById(R.id.view_pager);
		mViewPager.setAdapter(new SamplePagerAdapter(url));
	}

	class SamplePagerAdapter extends PagerAdapter {

		// private static final int[] sDrawables = { R.drawable.ic_launcher,
		// R.drawable.ic_launcher, R.drawable.ic_launcher,
		// R.drawable.ic_launcher, R.drawable.ic_launcher,
		// R.drawable.ic_launcher };
		private String[] url;
		private BitmapUtils bitmapUtils;

		public SamplePagerAdapter(String[] url) {
			this.url = url;
			bitmapUtils = BitmapHelp.getBitmapUtils(PhotoActivity.this);
		}

		@Override
		public int getCount() {
			return url.length;
		}

		@Override
		public View instantiateItem(ViewGroup container, int position) {
			PhotoView photoView = new PhotoView(container.getContext());
			// photoView.setImageResource(sDrawables[position]);
			bitmapUtils.display(photoView, url[position]);
			// Now just add PhotoView to ViewPager and return it
			container.addView(photoView, LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT);

			return photoView;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.viewpager_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		menuLockItem = menu.findItem(R.id.menu_lock);
		toggleLockBtnTitle();
		menuLockItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(MenuItem item) {
				toggleViewPagerScrolling();
				toggleLockBtnTitle();
				return true;
			}
		});
		return super.onPrepareOptionsMenu(menu);
	}

	private void toggleViewPagerScrolling() {
		if (isViewPagerActive()) {
			((HackyViewPager) mViewPager).toggleLock();
		}
	}

	private void toggleLockBtnTitle() {
		boolean isLocked = false;
		if (isViewPagerActive()) {
			isLocked = ((HackyViewPager) mViewPager).isLocked();
		}
		String title = (isLocked) ? getString(R.string.menu_unlock)
				: getString(R.string.menu_lock);
		if (menuLockItem != null) {
			menuLockItem.setTitle(title);
		}
	}

	private boolean isViewPagerActive() {
		return (mViewPager != null && mViewPager instanceof HackyViewPager);
	}

	@Override
	protected void onSaveInstanceState(@NonNull Bundle outState) {
		if (isViewPagerActive()) {
			outState.putBoolean(ISLOCKED_ARG,
					((HackyViewPager) mViewPager).isLocked());
		}
		super.onSaveInstanceState(outState);
	}
}
